<?php
include ("../lib/BLL/DmChucVuLopBLL.php");

///khai bao
$ktma="";
$ktten="";
$masua="";
$tt="";
$thongbao="";
$ma="";
$ten="";
$ds_cvl=array();
$chucvulopBLL = new DmChucVuLopBLL();
//$tieuchidauvao = new DmTieuChiDauVao();

/////////////////////////////
if (isset($_POST['timkiem'])) {
        $keysearch="";
		$where="";
		if(isset($_POST['txtkey']))
		{
			$keysearch=$_POST['txtkey'];
			$where="WHERE".$chucvulopBLL->buildSearchQueryLike($keysearch);
        }
    }
if(!isset($_POST['timkiem'])||($_POST['txtkey']=="")){
        $where="WHERE 1=1";
    }
    
/////////////////////////////
if(isset($_GET['edit']))
{
    if($_GET['edit']="t")
    {
        $thongbao="Đã Sửa";
    }else
    {
        $thongbao="Không sửa được";
    }    
}
/////////////////////////////
if(isset($_GET['del']))
{
    if($_GET['del']="t")
    {
        $thongbao="Đã Xóa";
    }else
    {
        $thongbao="Chưa xóa được";
    }
}
////////////////////////////
if(isset($_POST['delAll']))
{
    if(isset($_POST['checkall']))
    {
        $kq=$chucvulopBLL->deleteAll();
        if($kq)
        {
            $tb="Đã xoá!!!";
        }else
        {
            $tb="Không thể xoá!!!";
        }
    }
    if(isset($_POST['checkbox']))
    {
        $delete=$_POST['checkbox'];
        foreach($delete as $madm_chucvulop)
        {
            $kq=$chucvulopBLL->DelChucvulop($madm_chucvulop);
            if($kq)
            {
                $tb="Đã xoá!!!";
            }else
            {
                $tb="Không thể xoá!!!";
            }
        }
    }
}
//////////////////////////////////
if(isset($_GET['ma']))
{
    $madm_chucvulop=$_GET['ma'];
    $ds_cvl=$chucvulopBLL->getItemChucvulop($madm_chucvulop);
    $ma=$ds_cvl['madm_chucvulop'];
    $ten=$ds_cvl['ten_chucvulop'];

    $tt="readonly";
}
/////////////////////////////////
if(isset($_GET['xoa']))
{
    $madm_chucvulop=$_GET['xoa'];
    $kq=$chucvulopBLL->DelChucvulop($madm_chucvulop);
    if ($kq == true)
        {
            header("location: ?muc=nhom3&modul=chucvulop&del=t");
        }else
        {
            header("location: ?muc=nhom3&modul=chucvulop&del=f");       
        }
}
/////////////////////////////////
if(isset($_POST['luu']))
{
    $madm_chucvulop=$_POST['madm_chucvulop'];
    $ten_chucvulop=$_POST['ten_chucvulop'];
        
    if(isset($_POST['masua']) && $_POST['masua']=="")
    {
        $chucvulop=new DmChucVuLop();
        $chucvulop->SetChucvulop($madm_chucvulop,$ten_chucvulop);
       
        $kq=$chucvulopBLL->InsertChucvulop($chucvulop);
        
        if($kq==true){
            $thongbao="Thêm thành công";
        }else
        {      
            $thongbao="Thêm thất bại";
        }
    }else
    {
        $kq=$chucvulopBLL->UpdateChucvulop($madm_chucvulop,$ten_chucvulop);
        if ($kq == true)
        {
            header("location: ?muc=nhom3&modul=chucvulop&edit=t");
        }
        else
        {
            header("location: ?muc=nhom3&modul=chucvulop&edit=f");       
        }
    }
}

$ds_cvl=$chucvulopBLL->GetlistChucVuLop();
///// phân trang ///////
    $totalRecords=count($ds_cvl);           /// đếm số lượng bản gi
    $p=(isset($_GET['p']))?$_GET['p']:1;
    $limit=5;                           /// giởi hạn bản ghi đc hiện
    $offset=($p-1)*$limit;
    $ds_cvl=$chucvulopBLL->getPages($offset,$limit);
    $url="index.php?muc=nhom3&modul=chucvulop";
    $pages=$chucvulopBLL->paging($totalRecords,$limit,$url,'pagers');   /// gọi hàm phân trang

/////////////////////////////////////////////////////////
//$ds_nganh=$nganh_bll->timkiem($where);
$smarty->assign('p',$p);
$smarty->assign('pages',$pages);
$smarty->assign('ma',$ma);
$smarty->assign('ten',$ten);

$smarty->assign('tt',$tt);
$smarty->assign('thongbao',$thongbao);
$smarty->assign('ds_cvl',$ds_cvl);
$smarty->display('nhom3/chucvulop.tpl');
?>

